import MenuLocale from "./menu.locale";
import {i18n} from "plain-design";
import {globalComputed, reactive} from "@peryl/react-compose";
import {microApp} from "./microApp";

i18n.setLangs(MenuLocale);

export const MenuGroups = (() => {
  const state = reactive({
    data: ((): iAppMenuGroup[] => {
      return [
        {
          id: '01',
          icon: 'pi-apps',
          name: '自学使用',
          menus: [
            { id: '01', name: '流式调用', path: '/react-base/exercice/stream-request-page' },
            { id: '02', name: '流式调用-my', path: '/react-base/exercice-my/stream-request-page' },
          ]
        },
        {
          id: 'appMenu.statisticPanel',
          icon: 'pi-apps',
          name: 'appMenu.statisticPanel',
          menus: [
            { id: 'appMenu.dataStatistic', name: 'appMenu.dataStatistic', path: '/react-base/statistic-panel/data-statistic-page' },
            { id: 'appMenu.workspace', name: 'appMenu.workspace', path: '/react-base/statistic-panel/workspace-page' },
          ]
        },
        {
          id: 'appMenu.listPage',
          icon: 'pi-menu',
          name: 'appMenu.listPage',
          menus: [
            { id: 'appMenu.searchTable', name: 'appMenu.searchTable', path: 'react-base/demo-list/list-page' },
            { id: 'appMenu.cascadeList', name: 'appMenu.cascadeList', path: 'react-base/demo-list/cascade-list-page' },
            { id: 'appMenu.checkList', name: 'appMenu.checkList', path: 'react-base/demo-list/check-list-page' },
            { id: 'appMenu.expandList', name: 'appMenu.expandList', path: 'react-base/demo-list/expand-list-page' },
            { id: 'appMenu.mergeList', name: 'appMenu.mergeList', path: 'react-base/demo-list/merge-list-page' },
            { id: 'appMenu.treeList', name: 'appMenu.treeList', path: 'react-base/demo-list/tree-list-page' },
            { id: 'appMenu.sortList', name: 'appMenu.sortList', path: 'react-base/demo-list/sort-list-page' },
            { id: 'appMenu.virtualList', name: 'appMenu.virtualList', path: 'react-base/demo-list/virtual-list-page' },

          ]
        },
        {
          id: 'appMenu.formPage',
          icon: 'pi-idcard',
          name: 'appMenu.formPage',
          menus: [
            { id: 'appMenu.formElementsPage', name: 'appMenu.formElementsPage', path: 'react-base/demo-form/form-elements-page' },
            { id: 'appMenu.stepFormPage', name: 'appMenu.stepFormPage', path: 'react-base/demo-form/step-form-page' },
            { id: 'appMenu.groupFormPage', name: 'appMenu.groupFormPage', path: 'react-base/demo-form/group-form-page' },
            { id: 'appMenu.virtualFormSinglePage', name: 'appMenu.virtualFormSinglePage', path: 'react-base/demo-form/virtual-form-single-page' },
            { id: 'appMenu.virtualFormMultiplePage', name: 'appMenu.virtualFormMultiplePage', path: 'react-base/demo-form/virtual-form-multiple-page' },
            { id: 'appMenu.virtualFormGroupPage', name: 'appMenu.virtualFormGroupPage', path: 'react-base/demo-form/virtual-form-group-page' },
          ]
        },
        {
          id: 'appMenu.resultPage',
          icon: 'pi-desktop',
          name: 'appMenu.resultPage',
          menus: [
            { id: 'appMenu.resultSuccessPage', name: 'appMenu.resultSuccessPage', path: 'react-base/demo-result/result-success-page' },
            { id: 'appMenu.resultFailPage', name: 'appMenu.resultFailPage', path: 'react-base/demo-result/result-failure-page' },
          ]
        },
        {
          id: 'appMenu.errorPage',
          icon: 'pi-question-circle',
          name: 'appMenu.errorPage',
          menus: [
            { id: 'appMenu.errorPage403', name: 'appMenu.errorPage403', path: 'react-base/core/403-page' },
            { id: 'appMenu.errorPage404', name: 'appMenu.errorPage404', path: 'react-base/core/404-page' },
            { id: 'appMenu.errorPage500', name: 'appMenu.errorPage500', path: 'react-base/core/500-page' },
          ]
        },
        {
          id: 'appMenu.personal',
          icon: 'pi-user',
          name: 'appMenu.personal',
          menus: [
            { id: 'appMenu.userInfo', name: 'appMenu.userInfo', path: 'react-base/user/user-info-page' },
            { id: 'appMenu.userSetting', name: 'appMenu.userSetting', path: 'react-base/user/user-setting-page' },
          ]
        },
        {
          id: 'appMenu.internalTesting',
          icon: 'pi-bug',
          name: 'appMenu.internalTesting',
          menus: [
            { id: 'appMenu.mockRequest', name: 'appMenu.mockRequest', path: 'react-base/demo/request-interceptor-page' },
            { id: 'appMenu.predefineRoute', name: 'appMenu.predefineRoute', path: 'react-base/demo/predefine-route-page' },
            { id: 'appMenu.pathAnimation', name: 'appMenu.pathAnimation', path: 'react-base/demo/demo-path-animation-page' },
            { id: 'appMenu.stockLogger', name: 'appMenu.stockLogger', path: 'react-base/demo/demo-stock-logger-page' },
            { id: 'appMenu.stockInquiry', name: 'appMenu.stockInquiry', path: 'react-base/demo/demo-stock-query-generator-page' },
            { id: 'appMenu.pptParser', name: 'appMenu.pptParser', path: 'react-base/demo/demo-ppt-parser-page' },
          ]
        },
        {
          id: 'appMenu.dataManager',
          icon: 'pi-public',
          name: 'appMenu.dataManager',
          menus: [
            { id: 'appMenu.ovManager', name: 'appMenu.ovManager', path: 'react-base/data/ov-list-page' },
            { id: 'appMenu.productManager', name: 'appMenu.productManager', path: 'react-base/data/product-list-page' },
            { id: 'appMenu.urlMappingList', name: 'appMenu.urlMappingList', path: 'react-base/data/url-mapping-list-page' },
            { id: 'appMenu.cmsPageList', name: 'appMenu.cmsPageList', path: 'react-base/data/cms-page-list-page' },
            { id: 'appMenu.toolsPage', name: 'appMenu.toolsPage', path: 'react-base/data/tools-page' },
          ]
        },
        {
          id: 'appMenu.aiManager',
          icon: 'pi-common',
          name: 'appMenu.aiManager',
          menus: [
            { id: 'appMenu.aiListPage', name: 'appMenu.aiListPage', path: 'react-base/ai/ai-list-page' },
            { id: 'appMenu.aiFreeQuery', name: 'appMenu.aiFreeQuery', path: 'react-base/ai/ai-free-query-page' },
            { id: 'appMenu.aiVirtualForm', name: 'appMenu.aiVirtualForm', path: 'react-base/ai/ai-form-multiple-page' },
            { id: 'appMenu.aiVirtualGroupForm', name: 'appMenu.aiVirtualGroupForm', path: 'react-base/ai/ai-form-group-page' },
            { id: 'appMenu.aiFormValidate', name: 'appMenu.aiFormValidate', path: 'react-base/ai/ai-form-validate-page' },
            { id: 'appMenu.ai3d', name: 'appMenu.ai3d', path: 'react-base/ai/ai-3d-page' },
            { id: 'appMenu.aiLowCode', name: 'appMenu.aiLowCode', path: 'react-base/ai/ai-h5-dooring-page' },
            // { id: 'appMenu.aiResume', name: 'appMenu.aiResume', path: 'react-base/resume/resume-index-page' },
            { id: 'appMenu.aiResumeGenerator', name: 'appMenu.aiResumeGenerator', path: 'react-base/resume/resume-generator-list-page' },
            { id: 'appMenu.aiResumeTemplate', name: 'appMenu.aiResumeTemplate', path: 'react-base/resume/resume-template-page' },
            { id: 'appMenu.aiCodeGenerator', name: 'appMenu.aiCodeGenerator', path: 'react-base/code-generator/code-generator-list-page' },
            { id: 'appMenu.aiDecodeJsonStream', name: 'appMenu.aiDecodeJsonStream', path: 'react-base/ai/ai-stream-json-page' },
          ]
        },
        {
          id: 'appMenu.noCodeManager',
          icon: 'pi-code-square',
          name: 'appMenu.noCodeManager',
          menus: [
            { id: 'appMenu.entityTypes', name: 'appMenu.entityTypes', path: 'react-base/data/entity-list-page' },
            { id: 'appMenu.ruleManager', name: 'appMenu.ruleManager', path: 'react-base/data/rule-list-page' },
            { id: 'appMenu.cmsComponents', name: 'appMenu.cmsComponents', path: 'react-base/data/cms-component-list-page' },
            { id: 'appMenu.moduleList', name: 'appMenu.moduleList', path: 'react-base/data/module-list-page' },
            { id: 'appMenu.generalModule', name: 'appMenu.generalModule', path: 'react-base/data/general-module-page' },
          ],
        },
        /*{
          id: 'appMenu.systemManagement',
          icon: 'pi-settings',
          name: 'appMenu.systemManagement',
          menus: [
            { id: 'appMenu.menuManagement', name: 'appMenu.menuManagement', path: 'react-base/system/menu-list-page' },
          ]
        },*/
        {
          id: 'appMenu.microFront',
          icon: 'pi-apps',
          name: 'appMenu.microFront',
          menus: [
            { id: 'appMenu.microAppList', name: 'appMenu.microAppList', path: 'react-base/micro-front/micro-app-list-page' },
          ]
        },
      ];
    })(),
    id2map: globalComputed((): {
      menuId2group: Record<string, iAppMenuGroup | undefined>,
      menuId2menu: Record<string, iAppMenuItem | undefined>,
    } => {
      const menuId2group: Record<string, iAppMenuGroup | undefined> = {};
      const menuId2menu: Record<string, iAppMenuItem | undefined> = {};
      state.data.forEach(group => {
        group.menus.forEach(menu => {
          menuId2menu[menu.id] = menu;
          menuId2group[menu.id] = group;
        });
      });
      return { menuId2menu, menuId2group };
    }),
    openHomePage: async () => {
      const group = state.data[0];
      const menu = group.menus[0];
      microApp.openTab({ id: menu.id, icon: group.icon, path: menu.path, label: menu.name, tabData: { menu } });
    },
  });
  return state;
})();

export interface iAppMenuGroup {
  id: string,
  icon: string,
  name: string,
  menus: iAppMenuItem[],
}

export interface iAppMenuItem {
  id: string,
  name: string,
  path: string,
  data?: any,
  code?: string,
}

export function getMenuName(item: iAppMenuGroup | iAppMenuItem) {
  return i18n.$intl(item.name).d(item.name);
}

export const MenuUtils = {
  openMenu(menu: iAppMenuItem, group: iAppMenuGroup) {
    microApp.openTab({ id: menu.id, icon: group.icon, path: menu.path, label: menu.name, tabData: { menu }, data: menu.data });
  },
  openByMenuId(menuId: string): { menu: iAppMenuItem, group: iAppMenuGroup } | undefined {
    let result: { menu: iAppMenuItem, group: iAppMenuGroup } | undefined;
    MenuGroups.data.find(group => {
      const menu = group.menus.find(i => i.id == menuId);
      if (!!menu) {result = { group, menu };}
      return !!menu;
    });
    !!result && MenuUtils.openMenu(result.menu, result.group);
    return result;
  },
  openByMenuCode(menuCode: string) {
    let result: { menu: iAppMenuItem, group: iAppMenuGroup } | undefined;
    MenuGroups.data.find(group => {
      const menu = group.menus.find(i => i.code == menuCode);
      if (!!menu) {result = { group, menu };}
      return !!menu;
    });
    !!result && MenuUtils.openMenu(result.menu, result.group);
    return result;
  }
};
